import axios from 'axios'
// import { message } from 'ant-design-vue'
// import store from '@/store'
// import { getToken } from '@/utils/auth'
import router from '@/router/index.js'
import config from '@/config/index'

const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro

// create an axios instance
const service = axios.create({
    baseURL: baseUrl,
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
})

// 在这里做token的设置 request interceptor
/*service.interceptors.request.use(
  config => {
    let token = getToken();
    if (token) {
      config.headers['X-Access-Token'] = token
    }
    return config
  },
  error => {
    // do something with request error
    message.error("请求超时");
    console.log("request.js error", error) // for debug
    return Promise.reject(error)
  }
)*/

// response interceptor
service.interceptors.response.use(
    response => {
        let code = response.data.code;
        // status ---200
        let msg = response.data.msg;
        if (code === 200) {
            console.log("数据请求成功！！！")
            let data = response.data.list;
            return data;
        } else {
            // message.error(msg);
            console.log("数据请求失败！！！")
            console.log(msg);
            return Promise.reject(msg)
        }
    },
    error => {
        // 500报错拦截到这里了
        // console.log(error.response);
        let code = error.response.data.code;
        let msg = error.response.data.msg;
        // message.error(msg);
        alert(msg)
        if (code == 401) {
            router.push({
                name: 'Login'
            })
        }
        return Promise.reject(msg)
    }
)

export default service